home *** CD-ROM | disk | FTP | other *** search
/ ADA Programming Guide / ADA Programming Guide.iso / ada_gwu / prsutil.c < prev    next >
C/C++ Source or Header  |  1996-01-30  |  10KB  |  452 lines

  1. /*
  2.  * Copyright (C) 1985-1992  New York University
  3.  * 
  4.  * This file is part of the Ada/Ed-C system.  See the Ada/Ed README file for
  5.  * warranty (none) and distribution info and also the GNU General Public
  6.  * License for more details.
  7.  
  8.  */
  9.  
  10. #include "hdr.h"
  11. #include "ada.h"
  12. #include "stdlib.h"
  13. #include "adalexp.h"
  14. #include "miscp.h"
  15. #include "smiscp.h"
  16. #include "prsutilp.h"
  17.  
  18. int    stack_count(struct prsstack *ps)                        /*;stack_count*/
  19. {
  20.     int size = 0;
  21.  
  22.     while( ps != (struct prsstack *)0) {
  23.         ps = ps->prev;
  24.         size++;
  25.     }
  26.  
  27.     return (size);
  28. }
  29.  
  30. void copystack(struct two_pool *s, struct two_pool **head, int *size)
  31.                                                             /*;copystack*/
  32. {
  33.     /*    This procedure copies the stack s represented by a singly linked list
  34.      *  It returns a pointer to the head (top) of the new stack, as well as a
  35.      *  count of the size of the stack, returned in size
  36.      *  Note : lists in the front-end are circular, so the link of the last node
  37.      *       points to the head of the list.
  38.      */
  39.  
  40.     struct two_pool *next,
  41.     *tmp_tp;
  42.  
  43.     *size = 1;
  44.     /*    Copy head node     */
  45.     tmp_tp = *head = TALLOC ();
  46.     tmp_tp -> link = NULL;
  47.     tmp_tp -> val.state = s -> val.state;
  48.     /*  Copy rest of list     */
  49.     next = s -> link;
  50.     while (next != NULL) {
  51.         tmp_tp -> link = TALLOC ();
  52.         tmp_tp = tmp_tp -> link;
  53.         tmp_tp -> link = NULL;
  54.         tmp_tp -> val.state = next -> val.state;
  55.         next = next -> link;
  56.         (*size)++;
  57.     }
  58. }
  59.  
  60. void dump_stack(struct two_pool *s)                        /*;dump_stack*/
  61. {
  62.     int        count = 0;
  63. #ifdef DEBUG
  64.     if (trcopt) {
  65.         while (s != (struct two_pool *)0) {
  66.             count++;
  67.             fprintf (errfile, "%d %s", s -> val.state,
  68.               ( ((count%10) == 0) ? "\n":",") );
  69.             s = s -> link;
  70.         }
  71.         fprintf (errfile, " ======>size = %d<====== \n",count);
  72.     }
  73. #endif
  74. }
  75.  
  76. void dump_prssyms(struct two_pool *s)                    /*;dump_prssyms*/
  77. {
  78.     int        count = 0;
  79. #ifdef DEBUG
  80.     if (trcopt) {
  81.         while (s != (struct two_pool *)0) {
  82.             count++;
  83.             fprintf (errfile, "%s %s", TOKSTR(s -> val.state),
  84.               ( ((count%5) == 0) ? "\n":",") );
  85.             s = s -> link;
  86.         }
  87.         fprintf (errfile, " ======>size = %d<====== \n",count);
  88.     }
  89. #endif
  90. }
  91.  
  92. void dump_prsstack(struct prsstack *p)                    /*;dump_prsstack*/ 
  93. {
  94.     /* dump symbols of parse stack */
  95.     int count = 0;
  96. #ifdef DEBUG
  97.     if (trcopt) {
  98.         while (p!=(struct prsstack *)0) {
  99.             count++;
  100.             fprintf(errfile,"%s %c",TOKSTR(p->symbol),
  101.               ( (count%5 == 0) ? '\n' : ' ') );
  102.             p = p->prev;
  103.         }
  104.         fprintf(errfile,"     size = %d\n",count);
  105.     }
  106. #endif
  107. }
  108.  
  109. void dump_toksyms(char *s)                            /*;dump_toksyms*/
  110. {
  111.     int        t;
  112.  
  113. #ifdef DEBUG
  114.     if (trcopt) {
  115.         fprintf (errfile, "Dump of TOKSYMS [%s]\n", s);
  116.         for (t = n_TOKSYMS; t >= 0; t--) {
  117.             fprintf (errfile, "%s%s", TOKSTR(TOKSYMS[t]),
  118.               ((t % 15) == 14 ? "\n" : " "));
  119.         }
  120.         fprintf (errfile, "\n    < ===== >\n");
  121.     }
  122. #endif
  123. }
  124.  
  125.  
  126. void convtolower(char *s)                                /*;convtolower*/
  127. {
  128.     /* Convtolower: convert an entire string to lowercase. */
  129.     while (*s) {
  130.         if (isupper(*s))
  131.             *s = tolower(*s);
  132.         s++;
  133.     }
  134. }
  135.  
  136. void cand_clear()                                        /*;cand_clear*/
  137. {
  138.     /* cand_clear :     clear (reinitialize) all candidate sets to empty */
  139.     short x;
  140.  
  141.     for (x = DELETE; x <= SPELL_SUBST; x++) {
  142.         /* TBSL: free current candidates */
  143.         CANDIDATES[x] = NULL;
  144.         n_CANDIDATES[x] = 0;
  145.     }
  146. }
  147.  
  148. void dump_cand()                                        /*;dump_cand*/
  149. {
  150.     /* dump_cand :    dump contents of the candidates map to errfile */
  151.     short x,i;
  152.     struct cand *c;
  153.     char *sub;
  154.  
  155. #ifdef DEBUG
  156.     if (trcopt) {
  157.         for (x = DELETE; x <= SPELL_SUBST; x++) {
  158.             switch (x) {
  159.             case DELETE:
  160.             case MERGE:
  161.                 if (n_CANDIDATES[x] == 0)
  162.                     break;
  163.                 fprintf(errfile,"%s :\n",(x == DELETE ? "delete" : "merge"));
  164.                 c = CANDIDATES[x];
  165.                 i=0;
  166.                 while (c != (struct cand *)0) {
  167.                     fprintf(errfile,"[ %s %d ] %c",TOKSTR(c->token_sym),
  168.                       c->backup_toks+1,((++i % 5) == 0 ? '\n' : ' ') );
  169.                     c = c->next;
  170.                 }  /* end while */
  171.                 if (i%5 != 0) fprintf(errfile,"\n");
  172.                 break;
  173.             case INSERT:
  174.             case SUBST:
  175.             case SPELL_SUBST:
  176.                 if (n_CANDIDATES[x] == 0)
  177.                     break;
  178.                 if (x == INSERT) sub = "insert";
  179.                 else if (x == SUBST) sub = "subst";
  180.                 else sub = "spell_subst";
  181.  
  182.                 fprintf(errfile,"%s :\n",sub);
  183.                 c = CANDIDATES[x];
  184.                 i=0;
  185.                 while (c != (struct cand *)0) {
  186.                     fprintf(errfile,"[ %s %s %d ] %c",TOKSTR(c->token_sym),
  187.                       TOKSTR(c->t3),
  188.                       c->backup_toks+1,((++i % 4) == 0 ? '\n' : ' ') );
  189.                     c = c->next;
  190.                 }
  191.                 if (i%4 != 0) fprintf(errfile,"\n");
  192.                 break;
  193.             } /* end switch */
  194.         } /* end for */
  195.     }
  196. #endif
  197. }
  198.  
  199. struct prsstack *copytoken(struct prsstack *b)                /*;copytoken*/
  200. {
  201.     struct prsstack *a;
  202.     char *oldp, *newp;
  203.     int i;
  204.  
  205.     /* First get space for a */
  206.     a = PRSALLOC();
  207.     a->symbol = b->symbol;
  208.     a->prev = NULL;
  209.     if ( ISTOKEN(b) ) { /* copy token structure */
  210.  
  211.         a->ptr.token = TOKALLOC();
  212.         a->ptr.token->index = b->ptr.token->index;
  213.         a->ptr.token->span = b->ptr.token->span;
  214.     }
  215.     else {  /* copy ast structure */
  216.  
  217.         a->ptr.ast = node_new(N_KIND(b->ptr.ast));
  218.         oldp = (char *)(b->ptr.ast); 
  219.         newp = (char *)(a->ptr.ast);
  220.         for (i = 0; i < sizeof(Node); i++)
  221.             *newp++ = *oldp++;
  222.     }
  223.     /* end of macro definition */
  224.     return a;
  225. }
  226.  
  227. /* start allocation routines */
  228.  
  229. /* This file contains the allocation and freeing routines for various
  230.    structures */
  231. /* replace EMalloc by emalloc (misc.c)
  232.  * char *EMalloc(n)
  233.  */
  234.  
  235.  
  236. static struct prsstack *deadprsstack = (struct prsstack *)0;
  237. static void prsfree1(struct prsstack *, struct prsstack *);
  238. static void prsfree2(struct prsstack *, struct prsstack *);
  239. static struct prsstack * prsfakealloc();
  240. struct prsstack *(*prsalloc)(unsigned) =(struct prsstack *(*)(unsigned))emalloc;
  241. void (*prsfree)(struct prsstack *, struct prsstack *) = prsfree1;
  242.  
  243. static struct prsstack *prsfakealloc()                    /*;prsfakealloc*/
  244. {
  245.     struct prsstack *tmp;
  246.  
  247.     if (deadprsstack != (struct prsstack *)0) {
  248.         tmp = deadprsstack;
  249.         deadprsstack = deadprsstack->prev;
  250.         return(tmp);
  251.     }
  252.     prsalloc = (struct prsstack *(*)(unsigned)) emalloc;
  253.     prsfree = prsfree1;
  254.     return((struct prsstack *)emalloc(sizeof(struct prsstack)));
  255. }
  256.  
  257. static void prsfree1(struct prsstack *p, struct prsstack *q)    /*;prsfree1*/
  258. {
  259.     prsfree = prsfree2;
  260.     prsalloc = (struct prsstack *(*)(unsigned)) prsfakealloc;
  261.     q->prev = deadprsstack;
  262.     deadprsstack = p;
  263. }
  264.  
  265. static void prsfree2(struct prsstack *p, struct prsstack *q)    /*;prsfree2*/
  266. {
  267.     q->prev = deadprsstack;
  268.     deadprsstack = p;
  269. }
  270.  
  271. static struct two_pool *deadtwostack = (struct two_pool *)0;
  272. static void tfree1(struct two_pool *, struct two_pool *);
  273. static void tfree2(struct two_pool *, struct two_pool *);
  274. static struct two_pool *tfakealloc();
  275. struct two_pool *(*talloc)(unsigned) = (struct two_pool *(*)(unsigned)) emalloc;
  276. void (*tfree)(struct two_pool *, struct two_pool *) = tfree1;
  277.  
  278. static struct two_pool *tfakealloc()
  279. {
  280.     struct two_pool *tmp;
  281.  
  282.     if (deadtwostack != (struct two_pool *)0) {
  283.         tmp = deadtwostack;
  284.         deadtwostack = deadtwostack->link;
  285.         return(tmp);
  286.     }
  287.     talloc = (struct two_pool *(*)(unsigned)) emalloc;
  288.     tfree = tfree1;
  289.     return((struct two_pool *)emalloc(sizeof(struct two_pool)));
  290. }
  291.  
  292. static void tfree1(struct two_pool *p, struct two_pool *q)        /*;tfree1*/
  293. {
  294.     tfree = tfree2;
  295.     talloc = (struct two_pool *(*)(unsigned)) tfakealloc;
  296.     q->link = deadtwostack;
  297.     deadtwostack = p;
  298. }
  299.  
  300. static void tfree2(struct two_pool *p, struct two_pool *q)        /*;tfree2*/
  301. {
  302.     q->link = deadtwostack;
  303.     deadtwostack = p;
  304. }
  305.  
  306. static struct token *deadtokstack = (struct token *)0;
  307. static void tokfree1(struct token *);                        /*;tokfree1*/
  308. static void tokfree2(struct token *);                        /*;tokfree1*/
  309. static struct token *tokfakealloc();
  310. struct token *(*tokalloc)(unsigned) = (struct token *(*)(unsigned)) emalloc;
  311. void (*tokfree)(struct token *) = tokfree1;
  312.  
  313. static struct token *tokfakealloc()                        /*;tokfakealloc*/
  314. {
  315.     struct token *tmp;
  316.  
  317.     if (deadtokstack == (struct token *)0) {
  318.         tokalloc = (struct token *(*)(unsigned)) emalloc;
  319.         tokfree = tokfree1;
  320.         return((struct token *)emalloc(sizeof(struct token)));
  321.     }
  322.     tmp = deadtokstack;
  323.     deadtokstack = ((struct tmptok *)deadtokstack)->link;
  324.     return(tmp);
  325. }
  326.  
  327. static void tokfree1(struct token *p)                        /*;tokfree1*/
  328. {
  329.     ((struct tmptok *)p)->link = deadtokstack;
  330.     deadtokstack = p;
  331.     tokalloc = (struct token *(*)(unsigned)) tokfakealloc;
  332.     tokfree = tokfree2;
  333. }
  334.  
  335. static void tokfree2(struct token *p)                        /*;tokfree2*/
  336. {
  337.     ((struct tmptok *)p)->link = deadtokstack;
  338.     deadtokstack = p;
  339. }
  340.  
  341. /*
  342. static Node deadnodestack = (Node)0;
  343. */
  344.  
  345. void nodefree(Node p)                                /*;nodefree*/
  346. {
  347. #ifdef gargar
  348.     if (p != OPT_NODE && p != any_node && N_KIND(p) != as_free) {
  349.         ((struct tmpnode *)p)->link = deadnodestack;
  350.         ((struct tmpnode *)p)->kind = as_free;
  351.         deadnodestack = p;
  352.     }
  353. #endif
  354. }
  355.  
  356. #ifdef gargar
  357. static Node *deadaststack = (struct ast **)0;
  358.  
  359. void astfree(struct ast **p)                                    /*;astfree*/
  360. {
  361.     ((struct tmpast *)p)->link = deadaststack;
  362.     deadaststack = p;
  363. }
  364. #endif
  365.  
  366. /* Cand stuff is for error recovery */
  367.  
  368. struct cand *(*candalloc)(unsigned) = (struct cand *(*)(unsigned)) emalloc;
  369.  
  370. char *find_name(struct prsstack *x)                                /*;find_name*/
  371. {
  372.     if  (ISTOKEN(x)) return TOKSTR(x->ptr.token->index);
  373.     return namelist(x->symbol);
  374. }
  375.  
  376. void attrnum(Node node)                                        /*;attrnum*/
  377. {
  378.     /* convert attribute string to internal attribute code */
  379.     int        i;
  380.     char    *s, *name;
  381.     static char *attrnames[] = {
  382.         "ADDRESS",
  383.         "AFT",
  384.         "BASE",
  385.         "CALLABLE",
  386.         "CONSTRAINED",
  387.         "O_CONSTRAINED",
  388.         "T_CONSTRAINED",
  389.         "COUNT",
  390.         "DELTA",
  391.         "DIGITS",
  392.         "EMAX",
  393.         "EPSILON",
  394.         "FIRST",
  395.         "O_FIRST",
  396.         "T_FIRST",
  397.         "FIRST_BIT",
  398.         "FORE",
  399.         "IMAGE",
  400.         "LARGE",
  401.         "LAST",
  402.         "O_LAST",
  403.         "T_LAST",
  404.         "LAST_BIT",
  405.         "LENGTH",
  406.         "O_LENGTH",
  407.         "T_LENGTH",
  408.         "MACHINE_EMAX",
  409.         "MACHINE_EMIN",
  410.         "MACHINE_MANTISSA",
  411.         "MACHINE_OVERFLOWS",
  412.         "MACHINE_RADIX",
  413.         "MACHINE_ROUNDS",
  414.         "MANTISSA",
  415.         "POS",
  416.         "POSITION",
  417.         "PRED",
  418.         "RANGE",
  419.         "O_RANGE",
  420.         "T_RANGE",
  421.         "SAFE_EMAX",
  422.         "SAFE_LARGE",
  423.         "SAFE_SMALL",
  424.         "SIZE",
  425.         "O_SIZE",
  426.         "T_SIZE",
  427.         "SMALL",
  428.         "STORAGE_SIZE",
  429.         "SUCC",
  430.         "TERMINATED",
  431.         "VAL",
  432.         "VALUE",
  433.         "WIDTH",
  434.         0    };
  435.  
  436.     s = N_VAL(N_AST1(node));
  437.     for (i = 0;; i++) {
  438.         name = attrnames[i];
  439.         if (name == (char *)0) break;
  440.         if (streq(name, s)) {
  441.             /* on match, change ast1 node to hold number, not str*/
  442.             N_KIND(N_AST1(node)) = as_number;
  443.             N_VAL(N_AST1(node)) = (char *) i;
  444.             break;
  445.         }
  446.     }
  447. #ifdef IGNORE
  448.     printf("attribute %s\n", s);
  449.     chaos("unable to match attribute name");
  450. #endif
  451. }
  452.